using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using Campus.Entities;
using System.Data.Entity.Infrastructure;


namespace CampusSite.Models
{
    public class CampusSiteContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, add the following
        // code to the Application_Start method in your Global.asax file.
        // Note: this will destroy and re-create your database with every model change.
        // 
        // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<CampusSite.Models.CampusSiteContext>());

        public DbSet<Campus.Entities.Escuela> Escuelas { get; set; }

        public DbSet<Campus.Entities.Actividad> Actividades { get; set; }

        public DbSet<Campus.Entities.Estudiante> Estudiantes { get; set; }

        public DbSet<Campus.Entities.Grupo> Grupos { get; set; }

        public DbSet<Campus.Entities.ObraSocial> ObrasSociales { get; set; }

        public DbSet<Campus.Entities.Profesor> Profesores { get; set; }

        public DbSet<Campus.Entities.Responsable> Responsables { get; set; }

        public DbSet<Campus.Entities.Documentacion> Documentaciones { get; set; }

        public DbSet<Campus.Entities.FrecuenciaDePago> FrecuenciasDePago { get; set; }

        public DbSet<Campus.Entities.DiaDeLaSemana> DiasDeLaSemana { get; set; }

        public DbSet<Campus.Entities.Turno> Turnos { get; set; }

        public DbSet<Campus.Entities.Pago> Pagos { get; set; }
        
        public DbSet<Campus.Entities.PagoMensual> PagoMensuals { get; set; }

        public DbSet<Campus.Entities.PagoAnual> PagoAnuals { get; set; }

        public DbSet<Campus.Entities.User> Users { get; set; }

        public DbSet<Campus.Entities.Convocatoria> Convocatorias { get; set; }
        
        public DbSet<Campus.Entities.Equipo> Equipos { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            //Evita EF compare la version del modelo con la version de la BD PARA VERSION 4.1 EF            
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            modelBuilder.Entity<User>()
            .HasOptional<Responsable>(u => u.Responsable)
            .WithOptionalDependent(c => c.Usuario).Map(p => p.MapKey("ResponsableId"));
            //.WithOptionalPrincipal(c => c.Usuario).Map(p => p.MapKey("ResponsableId"));



           // modelBuilder.Entity<Responsable>()
           //.HasOptional<User>(u => u.Usuario)
           //.WithOptionalPrincipal(c => c.Responsable).Map(p => p.MapKey("UserId"));

           // //HasOptional(x => x.CurrentOwner).WithOptionalPrincipal(t => t.CurrentCar); //User contain the Customer FK

          //  modelBuilder.Entity<User>().HasOptional(x => x.Responsable).WithRequired(x => x.Usuario).Map(x => x.MapKey("UserId"));




            base.OnModelCreating(modelBuilder);

            //modelBuilder.Entity<Responsable>()
            //    .HasOptional(zm => zm.Usuario);
            ////modelBuilder.Entity<User>()
            ////    .HasKey(zmt => zmt.UserId);
            //modelBuilder.Entity<User>()
            //    .HasRequired(u => u.Responsable)
            //    .WithRequiredDependent(r => r.Usuario);

            
            //modelBuilder.Entity<Responsable>()
            //.HasOptional(r => r.Usuario )
            //.WithRequired(r => r.Responsable);

            modelBuilder.Entity<Responsable>()
            .HasOptional(x => x.Usuario)
            .WithOptionalPrincipal()
            .Map(x => x.MapKey("ResponsableId"));

             modelBuilder.Entity<User>()
            .HasOptional(x => x.Responsable)
            .WithOptionalPrincipal()
            .Map(x => x.MapKey("UserId"));


        }

        
        
    }
}